package com.zsy.yygh_hosp.controller.api;

import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.Schedule;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;
import com.atguigu.yygh.vo.hosp.ScheduleOrderVo;
import com.zsy.yygh.common.exception.YyghException;
import com.zsy.yygh.common.helper.HttpRequestHelper;
import com.zsy.yygh.common.result.Result;
import com.zsy.yygh.common.result.ResultCodeEnum;
import com.zsy.yygh.common.utils.MD5;
import com.zsy.yygh_hosp.service.DepartmentService;
import com.zsy.yygh_hosp.service.HospitalService;
import com.zsy.yygh_hosp.service.HospitalSetService;
import com.zsy.yygh_hosp.service.ScheduleService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private HospitalSetService hospitalSetService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private ScheduleService scheduleService;

    //删除排班
    @PostMapping("schedule/remove")
    public Result remove(HttpServletRequest request){
        //获取传递过来的医院信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        //医院编号 和 排班编号
        String hoscode=(String)map.get("hoscode");
        String hosScheduleId=(String)map.get("hosScheduleId");

        //TODO 签名校验
        scheduleService.remove(hoscode,hosScheduleId);
        return Result.ok();
    }
    //查询排班接口
    @PostMapping("schedule/list")
    public Result findSchedule(HttpServletRequest request){
        //获取传递过来的科室信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        //获取医院编号
        String hoscode=(String)map.get("hoscode");
        //获取科室编号
        String depcode =(String)map.get("depcode");
        //当前页 和 每页记录数
        int page=StringUtils.isEmpty(map.get("page")) ? 1 : Integer.parseInt((String)map.get("page"));
        int limit=StringUtils.isEmpty(map.get("limit")) ? 1 : Integer.parseInt((String)map.get("limit"));
        //TODO 签名验证
        ScheduleOrderVo scheduleOrderVo = new ScheduleOrderVo();
        scheduleOrderVo.setHoscode(hoscode);
        scheduleOrderVo.setDepcode(depcode);
        //调用service方法
        Page<Schedule> pageModel= scheduleService.findPageSchedule(page,limit,scheduleOrderVo);

        return Result.ok(pageModel);
    }
    //上传排版接口
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        //获取传递过来的医院信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);

        //TODO 签名校验
        scheduleService.save(map);
        return Result.ok();
    }
    //删除科室接口
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request){
        //获取传递过来的医院信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        //医院编号 和 科室编号
        String hoscode=(String)map.get("hoscode");
        String depcode=(String)map.get("depcode");
        //TODO 签名验证
        departmentService.remove(hoscode,depcode);
        return Result.ok();
    }

    //查询科室
    @PostMapping("department/list")
    public Result findDepartment(HttpServletRequest request){
        //获取传递过来的医院信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        //获取医院编号
        String hoscode=(String)map.get("hoscode");
        //当前页 和 每页记录数
        int page=StringUtils.isEmpty(map.get("page")) ? 1 : Integer.parseInt((String)map.get("page"));
        int limit=StringUtils.isEmpty(map.get("limit")) ? 1 : Integer.parseInt((String)map.get("limit"));
        //TODO 签名验证
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        //调用service方法
        Page<Department> pageModel= departmentService.findPageDepartment(page,limit,departmentQueryVo);

        return Result.ok(pageModel);
    }
    //上传医院接口
    @ApiOperation(value ="上传科室")
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
        //获取传递过来的医院信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        //获取医院编号
        String hoscode=(String)map.get("hoscode");
        //1.获取医院系统传递过来得签名，签名进行MD5加密
        String hospSign=(String)map.get("sign");
        //2.根据传递过来的医院编号，查询数据库，查询签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //3.根据数据库查询签名进行MD5加密
        String encrypt = MD5.encrypt(signKey);

        //4.判断签名是否一致
        if (!hospSign.equals(encrypt)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //调用service接口
        departmentService.save(map);


        return Result.ok();
    }
    //查询医院
    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest request){
        //获取传递过来的医院信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        //获取医院编号
        String hoscode=(String)map.get("hoscode");
        //1.获取医院系统传递过来得签名，签名进行MD5加密
        String hospSign=(String)map.get("sign");
        //2.根据传递过来的医院编号，查询数据库，查询签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //3.根据数据库查询签名进行MD5加密
        String encrypt = MD5.encrypt(signKey);

        //4.判断签名是否一致
        if (!hospSign.equals(encrypt)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //调用service方法实现根据医院编号查询
        Hospital hospital=hospitalService.getByHoscode(hoscode);
        return Result.ok(hospital);
    }
    //上传医院接口
    @PostMapping("saveHospital")
    public Result saveHosp(HttpServletRequest request){
        //获取传递过来的医院信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);

        //1.获取医院系统传递过来的签名，签名进行MD5加密
        String hospSign = (String)map.get("sign");
        //2.根据传递过来医院编码，查询数据库，查询签名
        String hoscode = (String)map.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        //3.把数据库查询签名进行MD5加密
        String encrypt = MD5.encrypt(signKey);
        //4.判断签名是否一致
        if (!hospSign.equals(encrypt)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //传输过程中“+”转换为了 “”，因此我们要转换回来
        String logoData=(String) map.get("logoData");
        logoData=logoData.replaceAll(" ","+");
        map.put("logoData",logoData);
        //调用service的方法
        hospitalService.save(map);
        return Result.ok();

    }
}
